package com.maimang.platform.common.utils;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname DateForEachUtil
 * @description <br>输出两个日期之间的天、周、月</br>
 * @date 2019-10-23 22:03
 * @email jintaozhao@qq.com
 */
public class DateForEachUtil {

	public static final int DAY = 1;       //日
	public static final int WEEK = 2;      //周
	public static final int MONTH = 3;     //月

	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	/**
	 * 遍历两个日期之间的日、周、月
	 *
	 * @param startDateStr 开始日期字符串
	 * @param endDateStr   结束字符串
	 * @param flag         遍历的标志，按日还是按周遍历    DAY:日  WEEK：周  MONTH：月
	 */
	public static List dateForEach(String startDateStr, String endDateStr, Integer flag) throws Exception {
		//将字符串类型的日期转换为Date类型并加载到Calendar中(有异常直接抛)
		Date startDate = sdf.parse(startDateStr);
		Date endDate = sdf.parse(endDateStr);

		return dateForEach(startDate, endDate, flag);
	}

	public static List dateForEach(Date startDate, Date endDate, Integer flag) throws Exception {
		//实例化Calendar(为什么要实例化两个Calendar，肯定是为了后面要用啊0.0)
		Calendar startCalendar = Calendar.getInstance();
		Calendar endCalendar = Calendar.getInstance();
		Calendar tempCalendar = Calendar.getInstance();
		//将两个日期加载到Calendar中
		startCalendar.setTime(startDate);
		endCalendar.setTime(endDate);
		List resultList = new ArrayList();
		//开始遍历
		switch (flag) {
			case DAY:
				//按日遍历
				while (true) {
					String currentDateStr = sdf.format(startCalendar.getTime());
					//日期加1
					startCalendar.add(Calendar.DAY_OF_MONTH, 1);
					//当前日期和结束日历日期比较，超过结束日期则终止
					if (startCalendar.after(endCalendar))
						break;
				}
				break;
			case WEEK:
				int weekOfMonth = 0;
				//按周输出
				while (true) {

					String firstDateStr = "", secondDateStr = "";

					if (startCalendar.after(endCalendar)) {
						tempCalendar = startCalendar;
						startCalendar = endCalendar;
						endCalendar = tempCalendar;
					}
					int week = startCalendar.get(Calendar.DAY_OF_WEEK);
					firstDateStr = sdf.format(startCalendar.getTime());
					//并非周一为第一天  周五为第一天
					if (weekOfMonth == 0) {
						startCalendar.add(Calendar.DAY_OF_MONTH, 5 - week);
					} else {
						startCalendar.add(Calendar.DAY_OF_MONTH, 6);
					}

					if (startCalendar.after(endCalendar)) {
						secondDateStr = sdf.format(endCalendar.getTime());
					} else {
						secondDateStr = sdf.format(startCalendar.getTime());
					}

					List<String> days = DateUtils.getDays(firstDateStr, secondDateStr, DateUtils.newFormatShort);
					if (days.size() > 0) {
						weekOfMonth++;
					} else {
						continue;
					}
					if (!DateUtils.parseDate(firstDateStr).before(startDate))
						resultList.add(days);
					startCalendar.add(Calendar.DAY_OF_MONTH, 1);
					if (startCalendar.equals(endCalendar)) {
						days = DateUtils.getDays(firstDateStr, secondDateStr, DateUtils.newFormatShort);
						resultList.add(days);
						break;
					}
					if (startCalendar.after(endCalendar)) {
						break;
					}
				}
				break;
			case MONTH:
				while (true) {
					//和按日输出差不多
					String currentDateStr = sdf.format(startCalendar.getTime()).substring(0, 7);
					//日期加1
					startCalendar.add(Calendar.MONTH, 1);
					//当前日期和结束日历日期比较，超过结束日期则终止
					if (startCalendar.after(endCalendar))
						break;
				}
		}
		return resultList;
	}

	//测试
	public static void main(String[] args) throws Exception {
		String startDateStr = "2019-11-01";
		String endDateStr = "2019-11-30";
		List resultMap = DateForEachUtil.dateForEach(startDateStr, endDateStr, WEEK);
		System.out.println(resultMap.size());
	}
}